package com.rayo.keyboxdemo;

import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.rayo.keyholder.data.KeyHolderData;
import com.rayo.keyholder.jni.KeyHolderJni;
import com.rayo.keyholder.serialport.KeyHolderPort;
import com.rayo.wallreader.jni.WallreaderJni;

public class ControllerActivity extends AppCompatActivity implements IControllerView,View.OnClickListener {

    private Button mBtnPort, mBtnAddress, mBtnGetInfo, mBtnOpen;
    private TextView mTvMsg;

    private ImageView mIvLoad, mIvResult;
    private Animation mAnimLoad;

    private ControllerPresenter presenter;

    private int yourChoice = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_controller);
        presenter = new ControllerPresenter(this,this);
        presenter.start(null);
        initView();
    }

    private void initView() {
        mBtnPort = findViewById(R.id.btn_port);
        mBtnAddress = findViewById(R.id.btn_controller);
        mBtnGetInfo = findViewById(R.id.btn_info);
        mBtnOpen = findViewById(R.id.btn_open);
        mBtnPort.setOnClickListener(this);
        mBtnAddress.setOnClickListener(this);
        mBtnGetInfo.setOnClickListener(this);
        mBtnOpen.setOnClickListener(this);

        mTvMsg = (TextView) findViewById(R.id.tv_msg);
        mTvMsg.setText(KeyHolderJni.getmInstance().version());

        mIvResult = (ImageView) findViewById(R.id.iv_result);
        mIvLoad = (ImageView) findViewById(R.id.iv_loading);

        mAnimLoad = AnimationUtils.loadAnimation(this, R.anim.loading);
        LinearInterpolator lin = new LinearInterpolator();
        mAnimLoad.setInterpolator(lin);

        mBtnPort.setText("TTYS0");
//        KeyHolderPort.getmInstance().setPort("dev/ttyS2");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_port:
                final View portDialogView = LayoutInflater.from(this)
                        .inflate(R.layout.dialog_port_control, null);
                AlertDialog.Builder portDialog = new AlertDialog.Builder(this);
                RadioGroup rgPort = portDialogView.findViewById(R.id.rg_port);
                final RadioButton rbTtys0 = portDialogView.findViewById(R.id.rb_ttys0);
                final RadioButton rbTtys1 = portDialogView.findViewById(R.id.rb_ttys1);
                final RadioButton rbTtys2 = portDialogView.findViewById(R.id.rb_ttys2);

                rgPort.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup radioGroup, int i) {
                        if (rbTtys0.isChecked()) {
                            yourChoice = 0;
                        } else if (rbTtys1.isChecked()) {
                            yourChoice = 1;
                        } else if (rbTtys2.isChecked()) {
                            yourChoice = 2;
                        } else {

                        }
                    }
                });

                portDialog.setView(portDialogView);
                portDialog.setTitle("Set Port");
                portDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                portDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (yourChoice) {
                            case 0:
                                mBtnPort.setText("TTYS0");
                                KeyHolderPort.getmInstance().setPort("dev/ttyS0");
                                break;
                            case 1:
                                mBtnPort.setText("TTYS1");
                                KeyHolderPort.getmInstance().setPort("dev/ttyS1");
                                break;
                            case 2:
                                mBtnPort.setText("TTYS2");
                                KeyHolderPort.getmInstance().setPort("dev/ttyS2");
                                break;
                            default:
                                break;
                        }
                    }
                });
                portDialog.show();
                break;
            case R.id.btn_controller:
                final View setControllerDialogView = LayoutInflater.from(this)
                        .inflate(R.layout.dialog_set_control, null);
                final EditText mEdtType = setControllerDialogView.findViewById(R.id.edt_type);
                final EditText mEdtAdress = setControllerDialogView.findViewById(R.id.edt_address);
                final EditText mEdtTime = setControllerDialogView.findViewById(R.id.edt_time);
                AlertDialog.Builder setControllerDialog = new AlertDialog.Builder(this);

                setControllerDialog.setView(setControllerDialogView);
                setControllerDialog.setTitle("Set Controller");
                setControllerDialog.setPositiveButton("Set", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        byte[] key = {(byte) Integer.parseInt(mEdtType.getText().toString()),
                                (byte) Integer.parseInt(mEdtAdress.getText().toString()),
                                (byte) Integer.parseInt(mEdtTime.getText().toString())};
                        presenter.senData(new KeyHolderData((byte) 0xFF, (byte) 0xFF,(byte) 0xFF, key));
                        dialogInterface.dismiss();
                    }
                });
                setControllerDialog.show();

                break;
            case R.id.btn_info:
                final View getInfoDialogView = LayoutInflater.from(this)
                        .inflate(R.layout.dialog_get_info, null);
                final EditText mEdtTypeInfo = getInfoDialogView.findViewById(R.id.edt_type);
                final EditText mEdtAdressInfo = getInfoDialogView.findViewById(R.id.edt_address);
                AlertDialog.Builder getInfoDialog = new AlertDialog.Builder(this);

                getInfoDialog.setView(getInfoDialogView);
                getInfoDialog.setTitle("Get Controller Info");
                getInfoDialog.setPositiveButton("Get", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        byte[] key = {};
                        presenter.senData(new KeyHolderData((byte)Integer.parseInt(mEdtTypeInfo.getText().toString()), (byte) Integer.parseInt(mEdtAdressInfo.getText().toString())
                                ,(byte) 1, key));
                        dialogInterface.dismiss();
                    }
                });
                getInfoDialog.show();
                break;
            case R.id.btn_open:
                final View openDialogView = LayoutInflater.from(this)
                        .inflate(R.layout.dialog_open, null);
                final EditText mEdtTypeOpen = openDialogView.findViewById(R.id.edt_type);
                final EditText mEdtAdressOpen = openDialogView.findViewById(R.id.edt_address);
                Button mBtnLock = openDialogView.findViewById(R.id.btn_lock);
                Button mBtnRed = openDialogView.findViewById(R.id.btn_red);
                Button mBtnGreen = openDialogView.findViewById(R.id.btn_green);
                Button mBtnBlue = openDialogView.findViewById(R.id.btn_blue);
                AlertDialog.Builder openDialog = new AlertDialog.Builder(this);
                mBtnLock.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        byte[] key = {1,0,0,0};
                        presenter.senData(new KeyHolderData((byte)Integer.parseInt(mEdtTypeOpen.getText().toString()), (byte) Integer.parseInt(mEdtAdressOpen.getText().toString())
                                ,(byte) 2, key));
                    }
                });
                mBtnRed.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        byte[] key = {0,1,0,0};
                        presenter.senData(new KeyHolderData((byte)Integer.parseInt(mEdtTypeOpen.getText().toString()), (byte) Integer.parseInt(mEdtAdressOpen.getText().toString())
                                ,(byte) 2, key));
                    }
                });
                mBtnGreen.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        byte[] key = {0,0,1,0};
                        presenter.senData(new KeyHolderData((byte)Integer.parseInt(mEdtTypeOpen.getText().toString()), (byte) Integer.parseInt(mEdtAdressOpen.getText().toString())
                                ,(byte) 2, key));
                    }
                });
                mBtnBlue.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        byte[] key = {0,0,0,1};
                        presenter.senData(new KeyHolderData((byte)Integer.parseInt(mEdtTypeOpen.getText().toString()), (byte) Integer.parseInt(mEdtAdressOpen.getText().toString())
                                ,(byte) 2, key));
                    }
                });

                openDialog.setView(openDialogView);
                openDialog.setTitle("Open Controller");
                openDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        dialogInterface.dismiss();
                    }
                });
                openDialog.show();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.destroy();
    }

    @Override
    public void showLoading(boolean isShow) {
        if (isShow) {
            mIvLoad.setVisibility(View.VISIBLE);
            if (mAnimLoad != null && mIvLoad != null) {
                mIvLoad.clearAnimation();
                mIvLoad.startAnimation(mAnimLoad);
            }
        } else {
            if (mAnimLoad != null && mIvLoad != null) {
                mIvLoad.clearAnimation();
            }
            if (mIvLoad != null) {
                mIvLoad.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void showResultImage(boolean isSuccess) {
        if (null != mIvResult) {
            if (isSuccess) {
                mIvResult.setImageResource(R.drawable.success);
            } else {
                mIvResult.setImageResource(R.drawable.failed);
            }
            mIvResult.setVisibility(View.VISIBLE);
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1500);
                        mIvResult.setVisibility(View.GONE);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    @Override
    public void setMsg(String txt) {
        mTvMsg.setText(mTvMsg.getText() + "\n" + txt);
    }

    @Override
    public void cleanMsg() {
        mTvMsg.setText("");
    }
}
